package com.fourth.controller;

import com.fourth.entity.Address;
import com.fourth.service.AddressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/front")
public class AddressController {

    @Resource
    private AddressService addressService;

    @Resource
    HttpSession httpSession;


    /**
     * 根据uId获取地址信息
     * @return
     */
    @RequestMapping("/queryAddressById")
    public List<Address> queryById(){

        //httpSession.setAttribute("uId",1);
        Integer uId = (Integer)httpSession.getAttribute("uId");
        System.out.println("uId:"+uId);

        List<Address> addresses = addressService.queryById(uId);
        return addresses;
    }

    /**
     * 根据aid修改当前地址为收货地址
     * @param aId
     * @return
     */
    @RequestMapping("/changeTakeDeliveryAddress")
    public int changeTakeDeliveryAddress(Integer aId){
        //httpSession.setAttribute("uId",1);
        Integer uId = (Integer)httpSession.getAttribute("uId");
        int i = addressService.changeTakeDeliveryAddress(uId, aId);
        return i;
    }

    @RequestMapping("/changeDefaultAddress")
    public int changeDefaultAddress(Integer aId){

        //httpSession.setAttribute("uId",1);//todo 自定的UID
        Integer uId = (Integer)httpSession.getAttribute("uId");
        return addressService.defaultAddress(uId, aId);
    }

    @RequestMapping("/insertAddress")
    public ModelAndView insertAddress(Address address, ModelAndView modelAndView){
       // httpSession.setAttribute("uId",3);//todo 自定的UID
        Integer uId = (Integer) httpSession.getAttribute("uId");
        address.setUId(uId);
        System.out.println("address:" + address);
        addressService.insertAddress(address,uId);
        modelAndView.setViewName("redirect:/settlement.html");
        return modelAndView;
    }



}
